﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Mimoza.NetworkPlugin
{
    public class ExecutePlugin : Mimoza.PluginLayer.IExecutePlugin
    {
        #region IExecutePlugin Members

        public string PluginId
        {
            get { return this.ToString(); }
        }

        public void Add(Mimoza.ObjectLayer.Policy policy)
        {
            Add(policy, GetData(policy.Data));
        }

        public void Remove(Mimoza.ObjectLayer.Policy policy)
        {
            Remove(policy, GetData(policy.Data));
        }

        public void Update(Mimoza.ObjectLayer.Policy policy)
        {
            Update(policy, GetData(policy.Data));
        }

        #endregion

        void Add(Mimoza.ObjectLayer.Policy policy, NetworkPluginData data)
        {
            Trace("Add", policy, data);
            ApplyNetworkSettings(data);
        }

        void Update(Mimoza.ObjectLayer.Policy policy, NetworkPluginData data)
        {
            Trace("Update", policy, data);
            ApplyNetworkSettings(data);
        }

        void Remove(Mimoza.ObjectLayer.Policy policy, NetworkPluginData data)
        {
            Trace("Remove", policy, data);
        }

        void ApplyNetworkSettings(NetworkPluginData data)
        {
            bool bNeedRestart = false;

            string[] adaptersNames = Transport.LocalInfo.GetAdapters().GetNames();
            for (int i = 0; i < adaptersNames.Length; i++)
            {
                if (ApplyAdapterInfo(adaptersNames[i], data))
                    bNeedRestart = true;
            }

            if (bNeedRestart) Mimoza.PluginLayer.PluginResultStore.PutRestartResult();
        }

        bool ApplyAdapterInfo(string adapterName, NetworkPluginData data)
        {
            Common.Logger.Log.Trace("Mimoza.NetworkPlugin.ApplyAdapterInfo adapterName - {0}.", adapterName);

            Mimoza.Common.Instrumental.Registry registry = new Mimoza.Common.Instrumental.Registry(
                "Comm\\" + adapterName + "\\Parms\\Tcpip");

            // DHCP options removed from setting page of plugin
            //object intvalue = data.IsDHCPActive ? 1 : 0;
            //registry.CheckAndRewrite("EnableDhcp", intvalue);
            
            if (data.IsGatewayDefined)
                registry.CheckAndRewrite("DefaultGateway", data.GatewayAddress.ToString());
            if (data.IsDnsDefined)
                registry.CheckAndRewrite("DNS", data.PrefferedDns);

            return registry.IsModified;
        }

        void Trace(string operationName, Mimoza.ObjectLayer.Policy policy, NetworkPluginData data)
        {
            Common.Logger.Log.Info("Mimoza.NetworkPlugin.ExecutePlugin." + operationName + 
                " Name - '" + policy.Name + "'.");
        }

        NetworkPluginData GetData(string data)
        {
            return NetworkPluginData.Deserialize(data);
        }
    }
}
